package org.catspaw.cherubim.persistence.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public final class SessionManager {

	private static String				implementorClass;
	private SessionManagerImplementor	implementor;

	private SessionManager() {
		if (implementorClass == null) {
			implementor = new DefaultSessionManagerImplementor();
		} else {
			try {
				this.implementor = (SessionManagerImplementor) Class.forName(implementorClass)
						.newInstance();
			} catch (InstantiationException e) {
				throw new IllegalArgumentException(e);
			} catch (IllegalAccessException e) {
				throw new IllegalArgumentException(e);
			} catch (ClassNotFoundException e) {
				throw new IllegalArgumentException(e);
			}
		}
	}

	public static SessionManager getInstance() {
		return Holder.INSTANCE;
	}

	public Session getSession(boolean allowCreate) {
		return implementor.getSession(allowCreate);
	}

	public Session getSession() throws HibernateException {
		return implementor.getSession();
	}

	public Session getCurrentSession() throws HibernateException {
		return implementor.getCurrentSession();
	}

	public Session openSession() throws HibernateException {
		return implementor.openSession();
	}

	public void closeCurrentSession() throws HibernateException {
		implementor.closeCurrentSession();
	}

	public SessionFactory getSessionFactory() {
		return implementor.getSessionFactory();
	}

	public static String getImplementorClass() {
		return implementorClass;
	}

	public static void setImplementorClass(String implementorClass) {
		SessionManager.implementorClass = implementorClass;
	}

	private static class Holder {

		private static final SessionManager	INSTANCE	= new SessionManager();
	}
}
